package com.huanxing.cloud.product.controller.app;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huanxing.cloud.common.core.util.Result;
import com.huanxing.cloud.product.api.dto.GoodsSpuSalesVolumeReqDTO;
import com.huanxing.cloud.product.api.entity.GoodsSpu;
import com.huanxing.cloud.product.service.IGoodsSpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品spu
 *
 * @author lijx
 * @since 2022/3/1 10:13
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/app/goodsspu")
@Tag(description = "app-goodsspu", name = "商品spu-API")
public class AppGoodsSpuController {

	private final IGoodsSpuService goodsSpuService;

	@Operation(summary = "商品列表")
	@GetMapping("/page")
	public Result page(Page page, GoodsSpu goodsSpu) {
		return Result.success(goodsSpuService.apiPage(page, goodsSpu));
	}

	@Operation(summary = "通过id查询商品")
	@GetMapping("/{id}")
	public Result getById(@PathVariable String id) {
		return Result.success(goodsSpuService.getApiSpuById(id));
	}

	@Operation(summary = "通过ids查询商品")
	@GetMapping("/list/{ids}")
	public Result<List<GoodsSpu>> getById(@PathVariable List<String> ids) {
		return Result.success(goodsSpuService.listByIds(ids));
	}

	@Operation(summary = "通过店铺ids查询商品")
	@GetMapping("/getListByShopIds/{ids}")
	public Result<List<GoodsSpu>> getListByShopIds(@PathVariable List<String> ids) {
		return Result.success(goodsSpuService.listByShopIds(ids));
	}

	@Operation(summary = "修改商品销量")
	@PostMapping("/update/sales-volume")
	public Result<Boolean> updateSalesVolume(@RequestBody List<GoodsSpuSalesVolumeReqDTO> list) {
		if (CollectionUtils.isEmpty(list)) {
			return Result.fail();
		}
		list.forEach(v -> goodsSpuService.updateSalesVolume(v.getSpuId(), v.getSalesVolume()));
		return Result.success();
	}

}
